package com.restaurant.application.login.support;

import com.google.common.collect.Lists;
import com.nebutown.generation.Convert;
import com.nebutown.generation.PickableBean;

import java.util.List;

public class LoginData implements PickableBean {

    public static final String FN_USERNAME = "userName";
    public static final String FN_PASSWORD = "password";

    public static final List<String> FIELD_NAMES = Lists.newArrayList(
            FN_USERNAME,
            FN_PASSWORD);

    private String userName;
    private String password;

    public LoginData(String userName, String password) {
        setUserName(userName);
        setPassword(password);
    }

    public LoginData() {
    }

    public LoginData duplicate() {
        LoginData result = new LoginData();
        result.userName = this.userName;
        result.password = this.password;
        return result;
    }

    @Override
    public String toString() {
        return Convert.toJson(this);
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String value) {
        this.userName = value;
    }

    public LoginData withUserName(String value) {
        setUserName(value);
        return this;
    }

    public LoginData withUserName(String value, String defaultValue) {
        setUserName(value == null ? defaultValue : value);
        return this;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String value) {
        this.password = value;
    }

    public LoginData withPassword(String value) {
        setPassword(value);
        return this;
    }

    public LoginData withPassword(String value, String defaultValue) {
        setPassword(value == null ? defaultValue : value);
        return this;
    }

    public Object getValueOf(String name) {
        switch (name) {
            case "userName":
                return getUserName();
            case "password":
                return getPassword();
        }
        return null;
    }

    public boolean hasField(String name) {
        return FIELD_NAMES.contains(name);
    }

    @Override
    public int hashCode() {
        return signature().hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof LoginData)) return false;
        return signature().equals(((LoginData) o).signature());
    }

    public String signature() {
        throw new RuntimeException("Bean LoginData has no signature defined, no hash code or equals possible.");
    }
}
